package com.gxa.security.security.filter;


import com.gxa.security.security.security.TokenHandler;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;

import javax.annotation.Resource;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * 授权
 */
public class TokenAuthorizationFilter extends BasicAuthenticationFilter {

    private TokenHandler tokenHandler;
    private RedisTemplate redisTemplate;

    //构造函数
    public TokenAuthorizationFilter(AuthenticationManager authenticationManager, TokenHandler tokenHandler, RedisTemplate redisTemplate) {
        //必须调用父类的BasicAuthenticationFilter方法
        super(authenticationManager);
        this.tokenHandler = tokenHandler;
        this.redisTemplate = redisTemplate;
    }

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
       //获取当前认证成功用户权限信息
        UsernamePasswordAuthenticationToken authRequest = getAuthentication(request);
        //判断如果有权限信息，放到权限上下文中
        if(authRequest != null) {
            SecurityContextHolder.getContext().setAuthentication(authRequest);
        }
        //放行
        chain.doFilter(request,response);
    }

    private UsernamePasswordAuthenticationToken getAuthentication(HttpServletRequest request) {
        //从header获取token
        String token = request.getHeader("Authorization");
        if(token != null) {
            //从token获取用户名
            String username = tokenHandler.getUserInfoFromToken(token);
            //从redis获取对应权限列表
            Set<String> permissionValueList = (HashSet<String>)redisTemplate.opsForValue().get(username);
            Collection<GrantedAuthority> authority = new ArrayList<>();
            Iterator<String> iterator = permissionValueList.iterator();
            while(iterator.hasNext()){
                String permissionValue = iterator.next();
                SimpleGrantedAuthority auth = new SimpleGrantedAuthority(permissionValue);
                authority.add(auth);
            }
//            for(String permissionValue : permissionValueList) {
//                SimpleGrantedAuthority auth = new SimpleGrantedAuthority(permissionValue);
//                authority.add(auth);
//            }
            return new UsernamePasswordAuthenticationToken(username,token,authority);
        }
        return null;
    }
}
